/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.unicon.autoservicio.view.bean;

import com.googlecode.gmaps4jsf.component.icon.Icon;
import com.googlecode.gmaps4jsf.component.map.Map;
import com.googlecode.gmaps4jsf.component.marker.Marker;
import com.googlecode.gmaps4jsf.component.window.HTMLInformationWindow;
import com.icesoft.faces.context.effects.JavascriptContext;
import com.unicon.autoservicio.dao.PedidoDAO;
import com.unicon.autoservicio.util.Util;
import com.unicon.sdc.ds.ws.TruckLocationBean;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseId;
import javax.servlet.http.HttpServletRequest;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;


/**
 *
 * @author JMENDOZA | 20140108 | Proy. 817
 * @author JMENDOZA | 20140210 | Proy. 817: Que tengo para hoy
 */
public class ConsultaMiMapaBean extends BaseManageBean {

  
    private List<TruckLocationBean> truckLocations;
    private String urlKml;
    private boolean RenderedMap = true;
    private String tiCon = "";
    
    private Map miMapa;
    private String orderCodeMap = "";
    private Date orderDateMap;

    
    //<editor-fold defaultstate="collapsed" desc="Contructor">
    public ConsultaMiMapaBean() throws ParseException, DatatypeConfigurationException {
        
        RenderedMap = false;
        JavascriptContext.includeLib("http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyDExeMSlW8bf6ifNYCXbZpuiT3nL3oVMes", FacesContext.getCurrentInstance()); 
        
        
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if(request!=null)
        {
            SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd");
            
            String orderDate_ = (String)((HttpServletRequest) request).getAttribute("orderDate");
            String orderCode_ = (String)((HttpServletRequest) request).getAttribute("orderCode");
                
            if((orderDate_!=null) && (orderCode_!=null) ){
                
                    this.orderDateMap = formatoDelTexto.parse(orderDate_);
                    this.orderCodeMap = orderCode_;
                    
              
            }    
        }
    }
    //</editor-fold>

    
    //<editor-fold defaultstate="collapsed" desc="Metodos ActionEvent">
    
    public boolean btnKmlActionPerformed(ActionEvent event) throws DatatypeConfigurationException {
                
        if (!event.getPhaseId().equals(PhaseId.INVOKE_APPLICATION)) {
            event.setPhaseId(PhaseId.INVOKE_APPLICATION);
            event.queue();
            return true;
        }
        
        
        
        return true;
    }
      
    
    
    //</editor-fold>    
  
    
    //<editor-fold defaultstate="collapsed" desc="Metodos">
   
     
  
    
    //</editor-fold>
    
    
    //<editor-fold defaultstate="collapsed" desc="Getter and Setter">
    
    public Map getMiMapa() {
        return miMapa;
    }

    public void setMiMapa(Map miMapa) throws ParseException, DatatypeConfigurationException {
        this.miMapa = miMapa;
        /*
        Date ordDate;
        String ordCode;
        SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd");

        ordCode = "239";
        ordDate = formatoDelTexto.parse("2012-08-15");
        */
        if((this.orderCodeMap!=null) && (this.orderCodeMap!=null) ){        
        
            // Generate a set of default map marker locations        
            XMLGregorianCalendar orderDate;
            orderDate = Util.dateToXMLGregorian(this.orderDateMap);
            
            
            PedidoDAO pedidoDao = new PedidoDAO();
            
            truckLocations = pedidoDao.getTruckLocations(
                                    orderDate,
                                    this.orderCodeMap, 
                                    "",  
                                    "", 
                                    "");
            
            if(!(truckLocations.size() > 0)) { return;}
                        
            miMapa.setLatitude("-12.072631");  
            miMapa.setLongitude("-76.9228360000"); 
            
            int cont = 0;
            for (TruckLocationBean truckLocation : truckLocations) {
                
                if((truckLocation.getLatutud() == null) || (truckLocation.getLongitud() == null)){
                    continue;
                }
                
                String longitud = String.valueOf(truckLocation.getLatutud());
                String latitud = String.valueOf(truckLocation.getLongitud());
                String span_ini = "<div style='text-align: center;font-weight: normal; font-size: 10px;margin-top: 2px;margin-bottom: 0px;margin-right: 0px;margin-left: 0px;'>";
                String span_fin = "</div>";
                cont += 1;
                String cont_ = "";
                String htmlStr = "";
                cont_ = "A" + String.valueOf(cont);
                Marker mark = null;
                HTMLInformationWindow window = null;
                Icon icon = null;
                
                mark = new Marker();
                mark.setLatitude(latitud);  
                mark.setLongitude(longitud);
                mark.setJsVariable(cont_);
                mark.setId(cont_);
                
                icon = new Icon();                
                icon.setId("icon" + cont_);
                
                window = new HTMLInformationWindow();
                window.setId("win" + cont_);
                htmlStr = " <div style='margin-top: 1px;margin-bottom: 1px;margin-right: 1px;margin-left: 1px;'> ";
                if(truckLocation.getTipo().toString().trim().equals("truc")){
                    htmlStr += span_ini + "Unidad: " + truckLocation.getTruckCode().toString().trim() + span_fin ;
                    htmlStr += span_ini + truckLocation.getDeEtapa().toString() + span_fin ;
                    switch (truckLocation.getNuEtapa().intValue()){
                        case 3:
                        case 4:
                            String strTEO = "";
                            if(truckLocation.getTeo() >= 30)
                            {
                                strTEO = "<span style=\"font-weight: bold;color: red\">" + String.valueOf(truckLocation.getTeo()) + "</span>";
                            }
                            else
                            {
                                strTEO = String.valueOf(truckLocation.getTeo());
                            }
                            htmlStr += span_ini + "TEO (min): " + strTEO + span_fin;
                            break;
                        case 1:
                        case 2:                            
                            htmlStr += span_ini + truckLocation.getDeCamion() + span_fin;
                            break;
                    }
                    
                    icon.setImageURL("./images/mionka_unicon.gif");
                    icon.setWidth("35");
                     
                }
                else
                {
                    
                    icon.setImageURL("http://maps.google.com/mapfiles/kml/paddle/pink-circle.png");
                    htmlStr += span_ini + truckLocation.getDeCamion() + span_fin;                    
                    icon.setWidth("30");                  
                    icon.setHeight("30");
                }
                
                htmlStr += "</div>";
                
                window.setHtmlText( htmlStr);
                
                
                
                mark.getChildren().add(icon);
                mark.getChildren().add(window);
                miMapa.getChildren().add(mark);                 
            }
        }
    }
    
      
    //</editor-fold>

    

   

  
     
     
     
}

